package com.sc.nft.service.impl;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.sc.nft.dao.ProductStockDao;
import com.sc.nft.dao.RegisterNeckCollectionDao;
import com.sc.nft.dao.StaticDataDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.entity.ProductStock;
import com.sc.nft.entity.RegisterNeckCollection;
import com.sc.nft.entity.StaticData;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.RegisterNeckCollectionVO;
import com.sc.nft.entity.vo.UserLoginVO;
import com.sc.nft.enums.SendSMSEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.CheckParameterService;
import com.sc.nft.service.OrderService;
import com.sc.nft.service.RegisterNeckCollectionService;
import com.sc.nft.service.UserInfoService;
import com.sc.nft.sup.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@RequiredArgsConstructor
@Service
public class RegisterNeckCollectionServiceImpl implements RegisterNeckCollectionService {
    private final UserInfoDao userInfoDao;

    private final UserInfoService userInfoService;

    private final CheckParameterService checkParameterService;

    private final RegisterNeckCollectionDao registerNeckCollectionDao;

    private final OrderService orderService;

    private final StaticDataDao staticDataDao;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public RegisterNeckCollectionVO registerNeckCollection(String userTel, String smsCode, Long smsId) {
        // 验证码效验
        checkParameterService.CheckVerificationcode(userTel,
                smsId,
                smsCode,
                SendSMSEnum.NECK.getCode());

        // 组装模型
        RegisterNeckCollectionVO registerNeckCollectionVO = new RegisterNeckCollectionVO();
        registerNeckCollectionVO.setUserTel(userTel);
        registerNeckCollectionVO.setUserInfo(userInfoDao.getOneByUserTel(userTel));
        StaticData staticData = staticDataDao.getByType("register_neck_collection");
        // 用户存在的情况
        if (registerNeckCollectionVO.getUserExist()) {
            registerNeckCollectionVO.getUserInfo().setIsSm(Boolean.TRUE);
            existingUser(registerNeckCollectionVO, staticData);
        }

        // 用户不存在的情况下,需要注册用户让后在发放.
        if (!registerNeckCollectionVO.getUserExist()) {
            userInfoService.registeUser(null, 1, userTel, registerNeckCollectionVO.getPass());
            registerNeckCollectionVO.setUserInfo(userInfoDao.getOneByUserTel(userTel));
            registerNeckCollectionVO.getUserInfo().setIsSm(Boolean.TRUE);
            existingUser(registerNeckCollectionVO, staticData);
        }
        return registerNeckCollectionVO;
    }
    private void existingUser(RegisterNeckCollectionVO registerNeckCollectionVO, StaticData staticData) {
        // 用户存在,验证奖品是否发放
        registerNeckCollectionVO.setRegisterNeckCollection(registerNeckCollectionDao.getOneByUserId(registerNeckCollectionVO.getUserInfo().getId()));
        if (registerNeckCollectionVO.getNeckExist()) {
            // 已经发放了.
            registerNeckCollectionVO.setIsNeck(Boolean.FALSE);
        } else {
            // 发放藏品
            neck(registerNeckCollectionVO, staticData);
        }
    }

    /**
     * 发放藏品
     *
     * @param registerNeckCollectionVO
     */
    private void neck(RegisterNeckCollectionVO registerNeckCollectionVO, StaticData staticData) {
        // 未发放,发放奖品并且记录到日志
        Long productId = Convert.toLong(staticData.getValue());
        orderService.receiveFreeCollection(registerNeckCollectionVO.getUserInfo(), productId, 1);
        RegisterNeckCollection registerNeckCollection = new RegisterNeckCollection();
        registerNeckCollection.setUserId(registerNeckCollectionVO.getUserInfo().getId());
        registerNeckCollection.setProductId(productId);
        registerNeckCollection.insert();
        registerNeckCollectionVO.setIsNeck(Boolean.TRUE);
    }
}
